package com.txzt.gaea.provide.service.impl;

import com.txzt.base.core.RtnInfo;
import com.txzt.gaea.api.client.AdminRemoteService;
import com.txzt.gaea.dto.DeptDTO;
import com.txzt.gaea.provide.service.IDeptService;
import com.txzt.gaea.vo.ZTreeNodeVO;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DeptServiceImpl implements IDeptService {

    @Value("${tool.code}")
    String toolCode;
    @Value("${tool.version}")
    String toolVersion;

    @Autowired
    private AdminRemoteService adminRemoteService;

    @Override
    public List<Long> getAllChildDeptIds(Long parentNodeId) {
        List<DeptModel> list = this.deptTreeList(parentNodeId);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }else {
            List<Long> deptIds = list.stream().map(DeptModel::getId).collect(Collectors.toList());
            deptIds.add(parentNodeId);
            return deptIds;
        }
    }

    private List<DeptModel> deptTreeList(Long parentNodeId){
        DeptDTO deptDTO = new DeptDTO();
        deptDTO.setParentNodeId(parentNodeId);
        deptDTO.setToolCode(toolCode);
        deptDTO.setToolVersion(toolVersion);
        RtnInfo<List<ZTreeNodeVO>> result = adminRemoteService.getDeptTree(deptDTO);
        if (result == null) {
            return null;
        }
        List<ZTreeNodeVO> list = result.getData();
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        List<DeptModel> deptList = new ArrayList<>();
        for (ZTreeNodeVO z : list) {
            if (StringUtils.equals(z.getId(), "0")) {
                continue;
            }
            DeptModel first = new DeptModel(Long.parseLong(z.getId()),z.getName(),Long.parseLong(z.getId()),z.getIsParent());
            if (z.getIsParent()) {
                first.setChildren(getChildNode(first));
            }
            deptList.add(first);
        }
        return deptList;
    }

    private List<DeptModel> getChildNode(DeptModel node){
        List<DeptModel> childList = new ArrayList<>();
        if (node.getIsParent()){
            //查询子集
            DeptDTO var1 = new DeptDTO();
            var1.setToolCode(toolCode);
            var1.setToolVersion(toolVersion);
            var1.setParentNodeId(node.getId());
            RtnInfo<List<ZTreeNodeVO>> result = adminRemoteService.getDeptTree(var1);
            if (result == null) {
                return childList;
            }
            List<ZTreeNodeVO> list = result.getData();
            if (CollectionUtils.isEmpty(list)) {
                return childList;
            }
            for (ZTreeNodeVO z : list){
                childList.add(new DeptModel(Long.parseLong(z.getId()),z.getName(),Long.parseLong(z.getId()),z.getIsParent()));
            }
        }
        for (DeptModel item : childList) {
            if (item.getIsParent()){
                item.setChildren(getChildNode(item));
            }
        }
        if(childList.size() == 0){
            return new ArrayList<>();
        }
        return childList;
    }

    @Data
    private static class DeptModel{
        private Long id;
        private String label;
        private Long value;
        private Boolean isParent;
        private List<DeptModel> children;

        public DeptModel() {
        }

        public DeptModel(Long id, String label, Long value, Boolean isParent) {
            this.id = id;
            this.label = label;
            this.value = value;
            this.isParent = isParent;
        }
    }
}
